package com.itheima.mydemo2.test2;

public class Test {
    public static void main(String[] args) {
       // Dog dog = new Dog();

        useAnimal(new Dog());
        //dog.watch();
        useAnimal(new Cat());
        useAnimal(new Pig());

    }
    public static void useAnimal(Animal animal){
        animal.eat();
        //使用instanceof判断
        if (animal instanceof Dog){
            Dog dog = (Dog)animal;
            dog.watch();
        }
        if (animal instanceof Cat){
            Cat cat = (Cat)animal;
            cat.sleep();
        }
        if (animal instanceof Pig){
            Pig pig = (Pig)animal;
            pig.run();
        }


        /**Cat cat =(Cat)animal;
        cat.sleep();

        Pig pig = (Pig)animal;
        pig.run();
         */
    }
}
